<?php

class Application_Model_ProductBuyOfferMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_ProductBuyOffer');
		}
		return $this->_dbTable;
	}
	
	public function insertAll(Application_Model_ProductBuyOffer $obj)
    {
        $data = array(
        	'product_buy_bid_id' => $obj->getProduct_buy_bid_id(),
            'offer' => $obj->getOffer(),
         	'offer_type'   => $obj->getOffer_type(),
        	'status'   => $obj->getStatus(),
        	'creation_time' => $obj->getCreation_time()
        );
        $this->getDbTable()->insert($data);
    }
    
	public function updateAll(Application_Model_ProductBuyOffer $obj)
    {
    	$db = $this->getDbTable();
    	
         $data = array(
         	'product_buy_bid_id' => $obj->getProduct_buy_bid_id(),
            'offer' => $obj->getOffer(),
         	'offer_type'   => $obj->getOffer_type(),
         	'status'   => $obj->getStatus(),
        	'creation_time' => $obj->getCreation_time()
        );
        $db->update($data,array('product_buy_offer_id = ?' => $obj->getProduct_buy_offer_id()));
    }
    
	public function deleteProductBuyOfferByPbBid($product_buy_bid_id)
    {
        $this->getDbTable()->delete(array('product_buy_bid_id = ?' => $product_buy_bid_id));
    }
    
	public function findByResult($select){
		$db = $this->getDbTable();
    	$resultSet = $db->fetchAll($select);
        $entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_ProductBuyOffer();
			$entry->setProduct_buy_offer_id($row->product_buy_offer_id)
				->setProduct_buy_bid_id($row->product_buy_bid_id)
				->setOffer($row->offer)
				->setOffer_type($row->offer_type)
				->setStatus($row->status)
				->setCreation_time($row->creation_time);
			$entries[] = $entry;
		}
		return $entries;
    }
    
	public function findAll()
    {
    	$db = $this->getDbTable();
        $select = $db->select()->from('product_buy_offer')
        	->order('creation_time DESC');
    	return $this->findByResult($select);
    }
    
	public function findByProduct_buy_bid_id($product_buy_bid_id)
    {
    	if($product_buy_bid_id != null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_buy_offer')
	             ->where('product_buy_bid_id = ?', $product_buy_bid_id);
	    	
	    	return $this->findByResult($select);
    	}
    }
    
	public function findByPbbIdOfferType($product_buy_bid_id, $offer_type)
    {
    	if($product_buy_bid_id != null){
	    	$db = $this->getDbTable();
	        $select = $db->select()->from('product_buy_offer')
	             ->where('product_buy_bid_id = ?', $product_buy_bid_id)
	             ->where('offer_type = ?', $offer_type);
	    	
	    	return $this->findByResult($select);
    	}
    }
}

